package com.oc.claimobs.bb.screens;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Screen;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

import com.oc.claimobs.bb.ScreenFlow;
import com.oc.claimobs.bb.screens.camera.CameraScreen;
import com.oc.claimobs.bb.screens.camera.PictureCallBack;
import com.oc.utils.Style;

public class PreviewPictureScreen extends MainScreen implements PictureCallBack {

	public PreviewPictureScreen() {
		super( MainScreen.NO_VERTICAL_SCROLLBAR |  MainScreen.NO_HORIZONTAL_SCROLL | MainScreen.NO_HORIZONTAL_SCROLLBAR);
		init();
	}
	
	private static final int IMAGE_SCALING = 4;
	
	private Bitmap backgroundBitmap;
	private VerticalFieldManager vfm = null;
	
	private ButtonField pictureButton = null;
	
	private byte[] picture = null;
	
	private CameraScreen cameraScreen = null;
	
	private boolean nopicture = true;

	
	private void init() {

		// The background image.
		backgroundBitmap = Bitmap.getBitmapResource("img/title_back.png");
		
		vfm = new VerticalFieldManager(
				VerticalFieldManager.NO_VERTICAL_SCROLL
						| VerticalFieldManager.NO_VERTICAL_SCROLLBAR
						| VerticalFieldManager.USE_ALL_WIDTH
						| VerticalFieldManager.NON_FOCUSABLE
						| VerticalFieldManager.USE_ALL_HEIGHT) {
			public void paint(Graphics graphics) {
				graphics.drawBitmap(0, 0, Style.getScreenWidht(), 40, backgroundBitmap, 0, 0);
				super.paint(graphics);
			}
		};

		initPreview();
		
		add(vfm);
		
		if(cameraScreen == null){
			cameraScreen = new CameraScreen(this);
		}
	}

	
	private void previewPicture(byte[] raw)
	{
		this.nopicture = false;
		
		this.picture = raw;
		
		vfm.delete(hfm1);
		vfm.delete(hfm2);
		
		initPreview();
	}
	
	private HorizontalFieldManager hfm1 = null;
	private HorizontalFieldManager hfm2 = null;
	
	private void initPreview(){

		
        hfm1 = new HorizontalFieldManager( Field.FIELD_HCENTER );
        hfm2 = new HorizontalFieldManager( Field.FIELD_HCENTER );
    
        Bitmap image;
        if(nopicture == true){
        	image = Bitmap.getBitmapResource("img/nopic.png");
        } else {
        	image =	Bitmap.createBitmapFromBytes( picture, 0, -1, IMAGE_SCALING );
        }
        
        BitmapField imageField = new BitmapField( image );
        imageField.setMargin(60,0,0,0);
        hfm1.add( imageField );
        
        ButtonField cancelButton = new ButtonField( "Capturar" );
        cancelButton.setChangeListener( new FieldChangeListener() {
			
			public void fieldChanged(Field field, int context) {
		        
		        UiApplication.getUiApplication().pushScreen( cameraScreen );
				
			}
		});
        
        //
        hfm2.add(cancelButton);
        
        
        if(nopicture == false){
	        ButtonField photoButton = new ButtonField( "Siguiente" );
	        photoButton.setChangeListener( new FieldChangeListener() {
				
				public void fieldChanged(Field field, int context) {
					
					if(!savePicture(picture))
					{
						return;
					}
					
					Screen screen = ScreenFlow.getInstance().getNext("");
					UiApplication.getUiApplication().pushScreen(screen);
					try {
						UiApplication.getUiApplication().popScreen(ScreenFlow.getInstance().getFOTO_PAGE());
					} catch (IllegalArgumentException e) {
					}
				}
			});
	        
            hfm2.add(photoButton);
        }
        
        //
        vfm.add( hfm1 );
        vfm.add( hfm2 );
	}
		

	                   
	
	public boolean savePicture(byte[] raw){
		try {
			ScreenFlow.getInstance().setLastPicture(raw);
			return true;
		} catch (Exception e) {
			System.out.println("ERROR " + e.getClass() + ":  " + e.getMessage());
		}
		return false;
	}

	protected boolean invokeAction(int action) {
		boolean handled = super.invokeAction(action);

		if (!handled) {
			switch (action) {
				case ACTION_INVOKE: // Trackball click.
				{
					
					return true;
				}
			}
		}
		return handled;
	}

	
	public void returnImageBytes(byte[] raw) {

		//Preview Pic
		previewPicture(raw);
		
		//Unload Screen
		UiApplication.getUiApplication().popScreen(cameraScreen);
		
	}
	
	public void close() {
		this.setDirty(false);
		super.close();
	}
}
